package com.woniu.teachmall04.controller;

import ch.qos.logback.core.model.Model;
import com.woniu.teachmall04.entity.User;
import com.woniu.teachmall04.service.UserService;
import jakarta.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

@Controller
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;

    // 登录页面
    @GetMapping("/login")
    public String loginPage() {
        return "login";
    }

    // 登录处理
    @PostMapping("/login")
    public String login(@RequestParam String account,
                        @RequestParam String password,
                        HttpSession session,
                        RedirectAttributes redirectAttributes) {
        User user = userService.login(account, password);
        if(user == null) {
            redirectAttributes.addFlashAttribute("error", "用户名或密码错误");
            return "redirect:/user/login";
        }
        session.setAttribute("user", user);
        return "redirect:/";
    }

    // 注册页面
    @GetMapping("/register")
    public String registerPage() {
        return "register";
    }

    // 注册处理
    @PostMapping("/register")
    public String register(@RequestParam String account,
                           @RequestParam String password,
                           @RequestParam String email,
                           @RequestParam String telphone,
                           RedirectAttributes redirectAttributes) {
        if(!userService.register(account, password, email, telphone)) {
            redirectAttributes.addFlashAttribute("error", "注册失败，用户名可能已存在");
            return "redirect:/user/register";
        }
        redirectAttributes.addFlashAttribute("success", "注册成功，请登录");
        return "redirect:/user/login";
    }

    // 个人信息页面
    @GetMapping("/profile")
    public String profilePage(HttpSession session, Model model) {
        User user = (User) session.getAttribute("user");
        model.addAttribute("user", user);
        return "profile";
    }

    // 更新个人信息
    @PostMapping("/update")
    public String updateProfile(User user, HttpSession session) {
        User currentUser = (User) session.getAttribute("user");
        user.setId(currentUser.getId());
        userService.updateUserInfo(user);
        session.setAttribute("user", userService.getUserById(user.getId()));
        return "redirect:/user/profile";
    }

    // 修改密码页面
    @GetMapping("/change-password")
    public String changePasswordPage() {
        return "change-password";
    }

    // 处理密码修改
    @PostMapping("/change-password")
    public String changePassword(@RequestParam String oldPassword,
                                 @RequestParam String newPassword,
                                 HttpSession session,
                                 RedirectAttributes redirectAttributes) {
        User user = (User) session.getAttribute("user");
        if(!userService.changePassword(user.getId(), oldPassword, newPassword)) {
            redirectAttributes.addFlashAttribute("error", "原密码不正确");
            return "redirect:/user/change-password";
        }
        redirectAttributes.addFlashAttribute("success", "密码修改成功");
        return "redirect:/user/profile";
    }

    // 上传头像
    @PostMapping("/upload-avatar")
    @ResponseBody
    public String uploadAvatar(@RequestParam("file") MultipartFile file) throws Exception {
        return userService.uploadAvatar(file);
    }

    // 退出登录
    @GetMapping("/logout")
    public String logout(HttpSession session) {
        session.invalidate();
        return "redirect:/";
    }
}